<template>
  <div class="comment-reply">
    <van-nav-bar
      :title="
        comment.reply_count > 0 ? `${comment.reply_count}条回复` : '暂无回复'
      "
    >
      <van-icon slot="left" @click="$emit('close')" name="cross"></van-icon>
    </van-nav-bar>

    <div class="scroll-wrap">
      <!-- 当前评论项 -->
      <CommentItem :comment="comment"></CommentItem>
      <!-- /当前评论项 -->

      <!-- 评论的回复列表 -->
      <van-cell title="全部回复"></van-cell>
      <CommentList
        type="c"
        :source="comment.com_id"
        :list="commentList"
      ></CommentList>
      <!-- 评论的回复列表 -->
    </div>

    <!-- 底部写评论按钮 -->
    <div class="post-wrap">
      <van-button @click="isPostShow = true" round size="small" class="post-btn"
        >写评论</van-button
      >
    </div>
    <!-- /底部写评论按钮 -->

    <!-- 发布回复评论 -->
    <van-popup v-model="isPostShow" position="bottom">
      <CommentPost
        :target="comment.com_id"
        @post-success="onPostSuccess"
      ></CommentPost>
    </van-popup>
    <!-- 发布回复评论 -->
  </div>
</template>

<script>
import CommentItem from './comment-item.vue'
import CommentList from './article-comment.vue'
import CommentPost from './comment-post.vue'
export default {
  name: 'CommentReply',
  data () {
    return {
      isPostShow: false,
      commentList: [] // 评论的回复列表
    }
  },
  props: {
    comment: {
      type: Object,
      required: true
    }
  },
  components: {
    CommentItem,
    CommentList,
    CommentPost
  },
  methods: {
    onPostSuccess (data) {
      // 更新回复的数量
      this.comment.reply_count++
      // 关闭弹层
      this.isPostShow = false
      // 讲最新回复的内容展示到列表的最上方
      this.commentList.unshift(data.new_obj)
    }
  }
}
</script>

<style scopend lang='less'>
.comment-reply {
  .scroll-wrap {
    position: fixed;
    top: 92px;
    left: 0;
    right: 0;
    bottom: 88px;
    overflow-y: auto;
  }
  .post-wrap {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 88px;
    display: flex;
    align-content: center;
    justify-content: center;
    background-color: #fff;
    .post-btn {
      width: 60%;
      border-top: 1px solid #d8d8d8;
    }
  }
}
</style>
